package com.fengye.common.validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;


/**
 * @author sunlei
 * @description 手机号校验实现类
 */
public class PhoneValidator implements ConstraintValidator<PhoneValidation, String> {
    private static final Logger LOGGER = LoggerFactory.getLogger(PhoneValidator.class);
    private static final String REGEX = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(147))\\d{8}$";

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        boolean result = false;
        try {
            result = Pattern.matches(REGEX, s);
        } catch (Exception e) {
            LOGGER.error("验证手机号格式时发生异常，异常信息：", e);
        }
        return result;
    }
}
